{include file="public/nheader"}
<div class="layui-fluid layui-bg-white">
    <form action="{:url()}" class="layui-form layui-form-fixed">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>产品名称
            </label>
            <div class="layui-input-block">
                <input type="text" name="title" required="" lay-verify="required" autocomplete="off" class="layui-input"
                    value="{$row.title|default=''}" placeholder="请填写产品名称">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>产品描述
            </label>
            <div class="layui-input-block">
                <input type="text" name="descs" required="" lay-verify="required" autocomplete="off" class="layui-input"
                    value="{$row.descs|default=''}" placeholder="请填写产品描述">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>品牌|类别
            </label>
            <div class="layui-input-block">
                <div style="display: flex;width: 100%;">
                    <select name="agentid" lay-filter="agentid" lay-search>
                        <option value="">请选择品牌</option>
                        {foreach $agent as $v}
                        <option value="{$v.id}" {if !empty($row.agentid) && $row.agentid==$v.id}selected{/if}>
                            {$v.nickname}</option>
                        {/foreach}
                    </select>
                    <select name="cate"  lay-search>
                        <option value="">请选择品牌分类</option>
                        {foreach $cate as $v}
                        <option value="{$v.id}" {if !empty($row.cate) && $row.cate==$v.id}selected{/if}>
                            {$v.name}[{if $v.isshow == 1}显示{else}隐藏{/if}]</option>
                        {/foreach}
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>销售价（单位:元）
            </label>
            <div class="layui-input-block">
                <input type="number" name="price" required="" lay-verify="required" autocomplete="off"
                    class="layui-input" value="{$row.price|default=''}" placeholder="请填写产品价格">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <font color="red">* </font>排序(值越大越靠前)
            </label>
            <div class="layui-input-block">
                <input type="number" name="sort" required="" lay-verify="required" autocomplete="off"
                    class="layui-input" value="{$row.sort|default='0'}" placeholder="">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                图片
            </label>
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="test1">上传图片</button>
                <div class="layui-upload-list">
                    <input type="hidden" value="{$row.images|default=''}" name="images" id="covers">
                    <img class="layui-upload-img" id="demo1" src="{$row.images|default=''}"
                        style="width: 200px;height: auto;">
                </div>
            </div>
        </div>
        <div class="layui-footer layui-form-item layui-center ">
            <input type="hidden" value="{$row['id']|default='0'}" name="id" />
            <button class="layui-btn" lay-filter="submitIframe" type="button" lay-submit>提交</button>
            <button class="layui-btn layui-btn-primary" type="button" sa-event="closeDialog">取消</button>
        </div>
    </form>
</div>
{include file="public/nfooter"}
<script>
    layui.use(['form', 'layer', 'upload'], function () {
        $ = layui.jquery;
        var form = layui.form
            , upload = layui.upload
            , element = layui.element
            , layer = layui.layer;
        //图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/base/upload_img'
            , accept: 'images'
            , before: function (obj) {
                layer.load(); //上传loading
            }, done: function (res) {
                //如果上传失败
                if (res.code == 1) {
                    layer.closeAll('loading'); //关闭loading
                    return layer.msg('图片上传失败，请重试');
                }
                if (res.code == 200) {
                    //上传成功
                    $('#demo1').attr('src', res.url); //图片链接（base64）
                    $('#covers').val(res.url);
                    layer.closeAll('loading'); //关闭loading
                    return layer.msg('图片上传成功');
                }
            }
            , error: function () {
                layer.closeAll('loading'); //关闭loading
                return layer.msg(res.msg);
            }
        });

        form.on('select(agentid)', function (data) {
            console.log(data);
            if (!data.value) {
                $('select[name="cate"]').html('');
                form.render();
                return false;
            }
            //发异步，把数据提交给php
            $.ajax({
                type: 'get',
                url: '/base/getcates',
                data: { id: data.value },
                success: function (res) {
                    if (res.code == 200) {
                        var str = '<option value="">请选择品牌分类</option>';
                        $.each(res.data, function (i, obj) {
                            var shows = '隐藏';
                            if(obj.isshow == 1){
                                shows = '显示';
                            }
                            str += '<option value="' + obj.id;
                            str += '">' + obj.name + "[" + shows + "]";
                            str += '</option>';
                        });
                        $('select[name="cate"]').html(str);
                        form.render();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 1000 });
                    }
                }
            });
            return false;
        });
    });
</script>